﻿#region Copyright (c) 11/24/2011, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Globalization;
using System.Windows.Controls;


namespace Amarok.Presentation
{
	/// <summary>
	/// This type provides a base class for type-safe implementations of validation rules.
	/// </summary>
	/// 
	/// <typeparam name="T">
	/// The type of value supplied to the validation rule.</typeparam>
	public abstract class ValidationRule<T> : ValidationRule
	{
		// static data
		private static readonly ValidationResult sSuccessResult = new ValidationResult(true, null);


		#region ++ Public Interface ++

		/// <summary>
		/// When overridden in a derived class, performs validation checks on a value.
		/// </summary>
		/// 
		/// <param name="value">
		/// The value from the binding target to check.</param>
		/// <param name="cultureInfo">
		/// The culture to use in this validation rule.</param>
		/// 
		/// <returns>
		/// A <see cref="T:System.Windows.Controls.ValidationResult"/> object.</returns>
		public sealed override ValidationResult Validate(Object value, CultureInfo cultureInfo)
		{
			return OnValidate((T)value, cultureInfo);
		}

		#endregion

		#region ## Overridable Methods ##

		/// <summary>
		/// When overridden in a derived class, performs validation checks on a value.
		/// </summary>
		/// 
		/// <param name="value">
		/// The value from the binding target to check.</param>
		/// <param name="cultureInfo">
		/// The culture to use in this validation rule.</param>
		/// 
		/// <returns>
		/// A <see cref="T:System.Windows.Controls.ValidationResult"/> object.</returns>
		protected abstract ValidationResult OnValidate(T value, CultureInfo cultureInfo);

		#endregion

		#region ## Protected Interface ##

		/// <summary>
		/// Creates a validation result that represents success.
		/// </summary>
		/// 
		/// <returns>
		/// A new validation result instance.</returns>
		protected ValidationResult Success()
		{
			return sSuccessResult;
		}

		/// <summary>
		/// Creates a validation result that represents failure.
		/// </summary>
		/// 
		/// <param name="message">
		/// An error message, which may contain placeholders.</param>
		/// <param name="arguments">
		/// An optional list of arguments to be formatted into the placeholders contained in the error message.</param>
		/// 
		/// <returns>
		/// A new validation result instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentException">
		/// An empty string was passed to a method that did not accept it as a valid argument.</exception>
		protected ValidationResult Failure(String message, params Object[] arguments)
		{
			Verify.NotEmpty(message, "message");

			var formattedMessage = String.Format(CultureInfo.CurrentCulture,
				message, arguments);

			return new ValidationResult(false, formattedMessage);
		}

		#endregion

	}
}
